package com.mifengkong.frwidget.titlebar;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mifengkong.frtools.util.FRDisplayMetrics;
import com.mifengkong.frtools.util.FRImageLoad;
import com.mifengkong.frtools.util.FRString;
import com.mifengkong.frwidget.R;


/**
 *
 */

public class TitleBarUIComponent extends RelativeLayout {

    public TitleBarUIComponent(Context context) {
        super(context);
    }

    public TitleBarUIComponent(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TitleBarUIComponent(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private TextView mTvTitleBarLeft;
    private TextView mTvTitleBarTitle;
    private TextView mTvTitleBarRight;
    private TextView mTvTitleBarClose;
    private ImageView mIvRight;
    private View mBottomView;
    private RelativeLayout message_layout;
    private ImageView message_icon;
    private TextView message_mark;

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mTvTitleBarLeft = (TextView) findViewById(R.id.lt_tv_titlebar_left);
        mTvTitleBarTitle = (TextView) findViewById(R.id.lt_tv_titlebar_title);
        mTvTitleBarRight = (TextView) findViewById(R.id.lt_tv_titlebar_right);
        mTvTitleBarClose = (TextView) findViewById(R.id.lt_tv_close);
        mIvRight = (ImageView) findViewById(R.id.lt_iv_right);
        mBottomView = findViewById(R.id.lt_tv_bottom_view);
        //这部分是那个消息的红点布局
        message_layout = (RelativeLayout) findViewById(R.id.title_message);
        message_icon = (ImageView) findViewById(R.id.title_message_icon);
        message_mark = (TextView) findViewById(R.id.title_message_mark);
    }

    public void initLeft(String text, OnClickListener listener) {
        mTvTitleBarLeft.setText(text);
        mTvTitleBarLeft.setOnClickListener(listener);
    }

    public void setLeftTextContent(String text) {
        mTvTitleBarLeft.setText(text);
    }

    public void setLeftTextBlackContent(String text) {
        mTvTitleBarLeft.setTextColor(Color.BLACK);
        mTvTitleBarLeft.setText(text);
    }

    public void setTitlePaddingBottom(int paddingBottom) {
        mTvTitleBarLeft.setPadding(0,0,0,paddingBottom);
    }


    public void setLeftBtnRes(int drawableRes) {
        mTvTitleBarLeft.setCompoundDrawablesWithIntrinsicBounds(drawableRes, 0, 0, 0);
    }

    public void initLeft(int drawableRes, OnClickListener listener) {
        mTvTitleBarLeft.setCompoundDrawablesWithIntrinsicBounds(drawableRes, 0, 0, 0);
        mTvTitleBarLeft.setOnClickListener(listener);
    }

    public void initLeftBlack(String text, int drawableRes, OnClickListener onClickListener) {
        mTvTitleBarLeft.setTextColor(Color.BLACK);
        mTvTitleBarLeft.setText(text);
        mTvTitleBarLeft.setCompoundDrawablesWithIntrinsicBounds(drawableRes, 0, 0, 0);
        mTvTitleBarLeft.setOnClickListener(onClickListener);
    }


    public void initLeftBack(OnClickListener listener) {
        mTvTitleBarLeft.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon_back_white, 0, 0, 0);
        mTvTitleBarLeft.setOnClickListener(listener);
    }

    public void initLeftBackBlack(OnClickListener listener) {
        mTvTitleBarLeft.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon_back_black, 0, 0, 0);
        mTvTitleBarLeft.setOnClickListener(listener);
    }

    public void initLeftBackWhite(int res,OnClickListener listener) {
        mTvTitleBarLeft.setCompoundDrawablesWithIntrinsicBounds(res, 0, 0, 0);
        mTvTitleBarLeft.setOnClickListener(listener);
    }

    public void initLeft(String text, int drawableRes, OnClickListener listener) {
        mTvTitleBarLeft.setText(text);
        mTvTitleBarLeft.setCompoundDrawablesWithIntrinsicBounds(0, 0, drawableRes, 0);
        mTvTitleBarLeft.setOnClickListener(listener);
    }

    public void initLeftWitchLeftMargin(String text, int drawableRes,int leftMargin ,OnClickListener listener) {
        RelativeLayout.LayoutParams layoutParams = (LayoutParams) mTvTitleBarLeft.getLayoutParams();
        layoutParams.leftMargin = leftMargin;
        mTvTitleBarLeft.setLayoutParams(layoutParams);
        mTvTitleBarLeft.setText(text);
        mTvTitleBarLeft.setCompoundDrawablesWithIntrinsicBounds(0, 0, drawableRes, 0);
        mTvTitleBarLeft.setOnClickListener(listener);
    }

    public void initLeftWithBlackContent(String text, int drawableRes, OnClickListener listener) {
        mTvTitleBarLeft.setTextColor(Color.BLACK);
        mTvTitleBarLeft.setText(text);
        mTvTitleBarLeft.setCompoundDrawablesWithIntrinsicBounds(0, 0, drawableRes, 0);
        mTvTitleBarLeft.setOnClickListener(listener);
    }

    public void setLeftVisible(boolean isVisible) {
        mTvTitleBarLeft.setVisibility(isVisible ? VISIBLE : GONE);
    }

    public void initClose(OnClickListener listener) {
        mTvTitleBarClose.setVisibility(View.VISIBLE);
        mTvTitleBarClose.setOnClickListener(listener);
    }

    public void initTitle(String text) {
        if (text != null)
            mTvTitleBarTitle.setText(text);
    }

    public void initTitleBlack(String text) {
        mTvTitleBarTitle.setTextColor(Color.BLACK);
        if (text != null)
            mTvTitleBarTitle.setText(text);
    }

    public void initTitleAndBoldFont(String text) {
        mTvTitleBarTitle.getPaint().setFakeBoldText(true);
        if (text != null)
            mTvTitleBarTitle.setText(text);
    }

    public void initTitleAndBoldFontForWhite(String text) {
        mTvTitleBarTitle.getPaint().setFakeBoldText(true);
        mTvTitleBarTitle.setTextColor(Color.WHITE);
        if (text != null)
            mTvTitleBarTitle.setText(text);
    }

    public void initTitle(int drawableRes) {
        mTvTitleBarTitle.setBackgroundResource(drawableRes);
    }

    public void initRight(String text, OnClickListener listener) {
        mTvTitleBarRight.setText(text);
        mTvTitleBarRight.setOnClickListener(listener);
    }

    public void initRightTextWithColor(String text, int color, OnClickListener listener) {
        mTvTitleBarRight.setText(text);
        mTvTitleBarRight.setTextColor(ContextCompat.getColor(getContext(), color));
        mTvTitleBarRight.setOnClickListener(listener);
    }

    //贷款列表的显示消息的titleBar 有红点提示
    public void initRightMessageAndMark(int resoruce, int messageCount, OnClickListener listener) {
        if (listener != null) {
            message_layout.setOnClickListener(listener);
        }
        message_layout.setVisibility(VISIBLE);
        message_icon.setImageResource(resoruce);
        if (messageCount <= 0) {
            message_mark.setVisibility(INVISIBLE);
        } else {
            message_mark.setVisibility(VISIBLE);
            message_mark.setText(String.valueOf(messageCount));
        }
    }

    //贷款列表的显示消息的titleBar 有红点提示
    public void initRightMessageCount(int messageCount) {
        if (messageCount <= 0) {
            message_mark.setVisibility(INVISIBLE);
        } else {
            message_mark.setVisibility(VISIBLE);
            message_mark.setText(String.valueOf(messageCount));
        }
    }

    public void initRightDialogWhite(OnClickListener listener) {
        initRight(R.drawable.icon_service_white, listener);
    }

    public void initRightDialogBlack(OnClickListener listener) {
        initRight(R.drawable.icon_service_black, listener);
    }

    public void initRightTvServiceWhite(OnClickListener listener) {
        initRight("客服", listener);
    }

    public void initRightTvServiceBlack(OnClickListener listener) {
        initRightBlack("客服", listener);
    }


    public void initRightBlack(String text, OnClickListener listener) {
        mTvTitleBarRight.setText(text);
        mTvTitleBarRight.setTextColor(Color.BLACK);
        mTvTitleBarRight.setOnClickListener(listener);
    }

    public void initRightWhite(String text, OnClickListener listener) {
        mTvTitleBarRight.setText(text);
        mTvTitleBarRight.setTextColor(Color.WHITE);
        mTvTitleBarRight.setOnClickListener(listener);
    }

    public void initRight(int drawableRes, OnClickListener listener) {
        mTvTitleBarRight.setCompoundDrawablesWithIntrinsicBounds(0, 0, drawableRes, 0);
        mTvTitleBarRight.setOnClickListener(listener);
    }

    public void initRightHelpCenterBlack(OnClickListener listener) {
        initRight(R.drawable.icon_service_black, listener);
    }

    public void initRightHelpCenter(OnClickListener listener) {
        initRight(R.drawable.icon_service_white, listener);
    }

    public void setRightVisible(boolean isVisible) {
        mTvTitleBarRight.setVisibility(isVisible ? VISIBLE : GONE);
    }

    public void setRightTextColor(int color) {
        mTvTitleBarRight.setTextColor(ContextCompat.getColor(getContext(), color));
    }

    public void initRight(String text, int drawableRes, OnClickListener listener) {
        mTvTitleBarRight.setText(text);
        mTvTitleBarRight.setCompoundDrawablesWithIntrinsicBounds(0, 0, drawableRes, 0);
        mTvTitleBarRight.setOnClickListener(listener);
    }

    public void setRightBtnRes(int drawableRes) {
        mTvTitleBarRight.setCompoundDrawablesWithIntrinsicBounds(0, 0, drawableRes, 0);
    }

    public void setImageViewRes(String imageUrl) {
        if (FRString.isEmpty(imageUrl)) {
            return;
        }
        if (imageUrl.contains(".gif")) {
            FRImageLoad.loadImageGif(FRString.valueOf(imageUrl), mIvRight);
        } else {
            FRImageLoad.loadImage(FRString.valueOf(imageUrl), mIvRight);
        }
    }

    /**
     * TitleBar右边的图片
     * @param drawableID
     * @param listener
     */
    public void setTitleRightImg(int drawableID,int widthI,int heightI ,OnClickListener listener){
        RelativeLayout.LayoutParams layoutParams = (LayoutParams) mIvRight.getLayoutParams();
        layoutParams.width = widthI;
        layoutParams.height = heightI;
        mIvRight.setLayoutParams(layoutParams);
        mIvRight.setImageResource(drawableID);
        mIvRight.setOnClickListener(listener);
    }

    public void setImageViewOnClickListener(OnClickListener onClickListener) {
        mIvRight.setOnClickListener(onClickListener);
    }

    public void setBottomViewVisiable(boolean visiable) {
        mBottomView.setVisibility(visiable ? VISIBLE : INVISIBLE);
    }

    public void setTitleBarHeight(int height) {
        ViewGroup.LayoutParams layoutParams = this.getLayoutParams();
        layoutParams.height = FRDisplayMetrics.dp2px(getContext(), height);
        this.setLayoutParams(layoutParams);
    }

    public void setTitleGravityCenter(){
        RelativeLayout.LayoutParams layoutParams = (LayoutParams) mTvTitleBarTitle.getLayoutParams();
        layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
        mTvTitleBarTitle.setLayoutParams(layoutParams);
    }

}